<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/custom/adminevents.php
#	FUNCTION:		Custom handler for the Admin Events page
#	AUTHOR:			Cameron Morrow
#	CREATED:		22/03/2006
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for added event
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (@$_POST["mode"]) {

	# Get params
	$id = @$_POST["e-id"] ? makeInteger($_POST["e-id"]) : false;
	$name = @$_POST["e-name"] ? slash($_POST["e-name"]) : "Unnamed";
	$type = @$_POST["e-type"] ? makeInteger($_POST["e-type"]) : "1";
	$date = @$_POST["e-date"] ? slash($_POST["e-date"]) : date("d/m/Y");
	$date = date("Y-m-d", strtotime(substr($date, 6, 4) . "-" . substr($date, 3, 2) . "-" . substr($date, 0, 2)));
	$user = @$_POST["e-user"] ? slash($_POST["e-user"]) : "";
	$desc = @$_POST["e-desc"] ? slash($_POST["e-desc"]) : "Description";
	$status = @$_POST["e-status"] ? makeInteger($_POST["e-status"]) : "1";

	# If id was passed, we're editing
	if (!$id) {

		$sql = "INSERT INTO " . $PROJECT_DB_TABLES["events"] . " (e_name, e_type, e_date, e_user, e_desc, e_status) VALUES ('" . $name . "', " . $type . ", '" . $date . "', '" . $user . "', '" . $desc . "', " . $status . ")";

		if (executeSQLQuery($sql)) {
			addMessage("<p>Event added.</p>");
		} else {
			addMessage("<p>An error occured and the event could not be added.</p>", 1);
		}
	} else {

		$sql = "UPDATE " . $PROJECT_DB_TABLES["events"] . " SET e_name = '" . $name . "', e_type = " . $type . ", e_date = '" . $date . "', e_user = '" . $user . "', e_desc = '" . $desc . "', e_status = " . $status . " WHERE e_id = " . $id . " LIMIT 1";

		if (executeSQLQuery($sql)) {
			addMessage("<p>Changes saved.</p>");
		} else {
			addMessage("<p>An error occured and the changes could not be saved.</p>", 1);
		}
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for finished event
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (@$_GET["done"]) {
	
	# ID
	$id = @$_GET["done"] ? makeInteger($_GET["done"]) : false;

	if ($id) {
		$sql = "UPDATE " . $PROJECT_DB_TABLES["events"] . " set e_status = 2 WHERE e_id = " . $id . " LIMIT 1";
		if (executeSQLQuery($sql)) {
			addMessage("<p>Event/task marked as complete</p>");
		} else {
			addMessage("<p>An error occured and the task could not be marked as complete.</p>");
		}
	} else {
	}
}


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Params
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Month
$month = @$_GET["month"] ? $_GET["month"] : date("Y-m");
$get_month = $month;
$year = substr($month, 0, strpos($month, "-"));
$month = substr($month, strpos($month, "-") + 1);


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Get events
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Get events for this month and before
$month_events = getRecord("SELECT events.*, users.u_login_name FROM " . $PROJECT_DB_TABLES["events"] . " as events " .
	"LEFT JOIN " . $PROJECT_DB_TABLES["users"] . " as users " .
	"ON events.e_user = users.u_id " .
	"WHERE e_date <= '" . $year . "-" . $month . "-31 23:59:00' AND e_status != 2 ORDER BY e_date ASC");

# Get events for user
$user_events = getRecord("SELECT events.* FROM " . $PROJECT_DB_TABLES["events"] . " as events WHERE e_user = '" . $USER -> getID() . "' AND e_date LIKE '" . $year . "-" . $month . "%' AND e_status != 2");

# Scripts
addContent("<script language=\"javascript\" type=\"text/javascript\" src=\"../assets/scripts/treebox/styles.js\"></script>");


# Urgent events
$urgent_events = array();
foreach ($month_events as $event) {

	if ($event["e_date"] <= date("Y-m-d") . " 23:59:59") {
		$urgent_events[] = $event;
	}
}
if (count($urgent_events) > 0) {
	addContent("<h2>Urgent Events:</h2>");
	addContent("<ul class=\"eventlist\">");
	foreach ($urgent_events as $count => $event) {
		addContent("<li>");

		# Controls
		addContent("<span class=\"controls\"><a href=\"?month=" . $get_month . "&done=" . $event["e_id"] . "\">Done</a></span>");

		# User assigned?
		addContent("<strong>");
		if ($event["e_user"] != "") {
			addContent($event["u_login_name"] . ": ");
		}

		addContent($event["e_name"] . " (" . date("d/m/Y", strtotime($event["e_date"])) . "):</strong><br />" . $event["e_desc"]);
		addContent("</li>");
	}
	addContent("</ul>");
}

# Draw Header
addContent("<h2>Events for " . date("F, Y", strtotime($year . "-" . $month)) . "</h2>");

# Next and prev months
$next_month = date("U", strtotime($year . "-" . $month . "-01 next month"));
$prev_month = date("U", strtotime($year . "-" . $month . "-01 previous month"));

addContent("<div class=\"calendarnp\">");
addContent("<div style=\"float: right;\"><a href=\"?month=" . date("Y-m", $next_month) . "\">" . date("F, Y", $next_month) . " &raquo;</a></div>");
addContent("<div style=\"float: left;\"><a href=\"?month=" . date("Y-m", $prev_month) . "\">&laquo; " . date("F, Y", $prev_month) . "</a></div>");
addContent("</div>");

# Get users that can be assigned this task
$user_list = getRecord("SELECT u_id, u_login_name FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_group = 2 OR u_group = 3 ORDER BY u_login_name ASC");
array_unshift($user_list, array("u_id" => "", "u_login_name" => "None"));

# Add table
addContent("<br />");
addContent("<table class=\"calendartable\" cellspacing=\"0\" style=\"width: 100%; margin: 0; padding: 0;\"><tr>");

# Calendar
addContent("<td class=\"colmain\">");
#addContent("<!--");
addContent("<div class=\"calendar\" id=\"calendar\">");

/****************************************************************************************
Find the day of the month that the Sunday of the current week is on
Note - if not on current month, get equivalent time next month

Firstly get the day, and check day is OK for the month (eg can't have 31st of Feb)
Then get the Sunday that comes before that day
****************************************************************************************/

# Current day
$sunday_day = date("d");

# Transpose day to current month
if ($sunday_day > date("t", strtotime($year . "-" . $month))) {
	$sunday_day = date("t", strtotime($year . "-" . $month));
}

# Get the previous sunday
$sunday = date("d", strtotime($year . "-" . $month . "-" . $sunday_day . " last Sunday"));


/****************************************************************************************
Print the names of the days - Sun through Sat
****************************************************************************************/

# Print day names
foreach ($DAY_NAMES as $i => $day) {
	addContent("<div class=\"dayname\">" . $day . "</div>");
}

/****************************************************************************************
Print the days - including any initial padding to push the 1st to it's correct day
****************************************************************************************/

# Get number of days in this month
$days_in_month = date("t", strtotime($year . "-" . $month . "-01"));

# Find out what the first of the month is, and pad as neccessary
$first_day = date("w", strtotime($year . "-" . $month . "-01"));

# For each day in the month
for ($i = -$first_day + 1; $i <= $days_in_month; $i++) {

	# If still padding, add a padder now
	if ($i < 1) {

		addContent("<div class=\"daypad\">&nbsp;</div>");

	# Otherwise print a day
	} else {
	
		# Get information about this specific day
		$day = date("Y-m-d", strtotime($year . "-" . $month . "-" . str_pad($i, 2, "0", STR_PAD_LEFT)));

		/****************************************************************************************
		Determine the class and onclick of the day
		****************************************************************************************/

		# Default is a regular day
		$class = "day";
		if (date("w", strtotime($day)) == 0) {
			$prev_sunday = date("m-d", strtotime($day));
		} else {
			$prev_sunday = date("m-d", strtotime($day . " previous Sunday"));
		}
		$next_sunday = date("m-d", strtotime($day . " next Sunday"));
		$onclick = "openWeek('" . date("m-d", strtotime($day)) . "', '" . $prev_sunday . "', '" . $next_sunday . "');";

		# If it is outside the current week, it's a small day
		if ($i < $sunday || $i > $sunday + 6) {
			$class = "smallday";
		}

		# If it is day, make it so
		if ($day == date("Y-m-d")) {
			$class .= " today";
		}

		# Create DIV, Header, and Content holder
		addContent("<div id=\"day" . $i . "\" onclick=\"" . $onclick . "\" class=\"" . $class . "\"><h3>" . str_pad($i, 2, "0", STR_PAD_LEFT) . "</h3><div class=\"daycontent\">");

		/****************************************************************************************
		Day Tasks
		****************************************************************************************/

		# Get tasks for this day
		$tasks = array();
		foreach ($month_events as $event) {
			if (substr($event["e_date"], 0, 10) == $day) {
				$tasks[] = $event;
			}
		}

		# Create summary for small days
		if (count($tasks) > 0) {
			addContent("<div class=\"tasksum\">" . count($tasks) . " Items</div>");
		}

		# For each task
		addContent("<ul>");

		# Add an event item
		if (date("U", strtotime($day)) >= date("U", strtotime(date("Y-m-d")))) {
			addContent("<li class=\"addevent\"><a href=\"#form\" onclick=\"editCalendarDetails(false, '" . date("d/m/Y", strtotime($day)) . "');\">+</a></li>");
		}
		

		# For each task for today
		foreach ($tasks as $task) {
			addContent("<li id=\"task" . $task["e_id"] . "\" class=\"status" . $task["e_status"] . " nodetails\" onmouseover=\"showCalendarDetails(this.id);\" onmouseout=\"hideCalendarDetails(this.id);\">");
			
			# Store params
			addContent("<div class=\"hidden\">" . $task["e_type"] . "</div>");
			addContent("<div class=\"hidden\">" . $task["e_name"] . "</div>");
			addContent("<div class=\"hidden\">" . $task["e_user"] . "</div>");
			addContent("<div class=\"hidden\">" . date("d/m/Y", strtotime($task["e_date"])) . "</div>");
			addContent("<div class=\"hidden\">" . $task["e_desc"] . "</div>");
			addContent("<div class=\"hidden\">" . $task["e_status"] . "</div>");

			# Header
			addContent("<div class=\"taskheader\">");

			# Controls
			switch ($task["e_status"]) {
				case "1":
				case "2":
				case "3":
				case "4":
					addContent("<span class=\"markdone\"><a href=\"#form\" onclick=\"editCalendarDetails(" . $task["e_id"] . ");\">Edit</a> / <a href=\"?month=" . $get_month . "&done=" . $task["e_id"] . "\">Done</a></span>");
					break;
				default:
					break;
			}
			
			addContent("<strong>Task:</strong>");
			
			addContent("</div>");


			# Name of user and task
			addContent("<p>");

			# Name of user (if provided)?
			if ($task["e_user"] != "") {
				addContent("<strong>" . $task["u_login_name"] . ": </strong>");
			}
			
			# Name of task
			addContent($task["e_name"] . "</p>");

			# Description
			addContent("<p class=\"taskcontent\">" . $task["e_desc"] . "</p>");
			
			# End task
			addContent("</li>");
		}

		addContent("</ul>");
		addContent("</div></div>");
	}
}

# End Calendar
addContent("</div>");
addContent("</td>");

# Input form
addContent("<td id=\"addeventtd\" class=\"coladdoff\" valign=\"top\">");
addContent("<div id=\"addevent\">");
addContent("<h2>Add an event/task:</h2>");
addContent("<a name=\"form\"></a>");
addContent("<form name=\"addeventform\" id=\"addinputform\" action=\"adminevents.php\" method=\"post\">" .
	"<input type=\"hidden\" id=\"mode\" name=\"mode\" value=\"add\" />" .
	"<input type=\"hidden\" id=\"e-id\" name=\"e-id\" value=\"\" />" .

		# Name
		"<p>Event Name:<br />" .
		"<input type=\"text\" size=\"40\" name=\"e-name\" id=\"e-name\" value=\"\" maxlength=\"60\" /></p>" .

		# User
		"<p>User (Admin): <br />" .
		createSelector("e-user", extractColumnFromArray($user_list, "u_login_name"), extractColumnFromArray($user_list, "u_id")) . "</p>" .

		# Type
		"<p>Type:<br />" .
		createSelector("e-type", array_values($EVENT_TYPES), array_keys($EVENT_TYPES)) . "</p>" .

		# Date
		"<p>Date (DD/MM/YYYY):<br />" .
		"<input type=\"text\" size=\"40\" name=\"e-date\" id=\"e-date\" value=\"\" maxlength=\"60\" /></p>" .

		# Desc
		"<p>Desc:<br /><textarea name=\"e-desc\" id=\"e-desc\" rows=\"4\" cols=\"50\"></textarea></p>" .

		# Status
		"<p>Status:<br />" .
		createSelector("e-status", array_values($EVENT_STATUS), array_keys($EVENT_STATUS)) . "</p>" .

		# Buttons
		"<p><input type=\"button\" value=\"Cancel\" onclick=\"cancelEditEvent();\" class=\"savebutton\" /></p><p><input class=\"savebutton\" type=\"submit\" value=\"Save\" /></p>" .

"</form>");
addContent("</div>");
addContent("</td>");

# End table
addContent("</tr></table>");

# End calendar
addContent("<div style=\"clear: both;\"></div>");

# Show Tasks
addContent("<h2>Monthly tasks for " . $USER -> getLoginName() . ":</h2>");

if (count($user_events) > 0) {
	addContent("<ul class=\"eventlist\">");
	foreach ($user_events as $count => $event) {
		addContent("<li>");

		# Controls
		addContent("<span class=\"controls\"><a href=\"?month=" . $get_month . "&done=" . $event["e_id"] . "\">Done</a></span>");

		addContent("<strong>" . $event["e_name"] . " (" . date("d/m/Y", strtotime($event["e_date"])) . "):</strong><br />" . $event["e_desc"]);
		addContent("</li>");
	}
	addContent("</ul>");
} else {
	addContent("<p><em>No currently pending tasks for you in this month.</em></p>");
}

?>